package com.dy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig{
    /**
     * 配置跨域请求
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        //初始化cors配置对象
        CorsConfiguration corsConfiguraction = new CorsConfiguration();
        //允许跨域的域名
        corsConfiguraction.addAllowedOrigin("*");
        //允许cookie
        corsConfiguraction.setAllowCredentials(true);
        //可跨域的请求方式 如get put,post....
        corsConfiguraction.addAllowedMethod("*");
        //允许携带任何头信息
        corsConfiguraction.addAllowedHeader("*");
        //初始化cors配置源对象
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguraction);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }

}